package com.ssy.lingxi.system.service.impl;

import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.PageModel;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.sms.api.feign.IConfigSmsFeign;
import com.ssy.lingxi.sms.api.model.SmsConfigVO;
import com.ssy.lingxi.sms.api.model.request.DeleteConfigRequest;
import com.ssy.lingxi.system.dao.CommonParametersRepository;
import com.ssy.lingxi.system.dto.request.CommonParametersAddRequest;
import com.ssy.lingxi.system.dto.request.CommonParametersDeteleRequest;
import com.ssy.lingxi.system.dto.request.CommonParametersUpdateRequest;
import com.ssy.lingxi.system.dto.response.MiddlegroundUserLoginResponse;
import com.ssy.lingxi.system.entity.CommonParameters;
import com.ssy.lingxi.system.service.CommonParametersService;
import com.ssy.lingxi.system.utils.UserDetailsUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 短信配置
 * @author Administrator
 */
@Service
public class CommonParametersServiceImpl  implements CommonParametersService {

    @Resource
    UserDetailsUtil userDetailsUtil;
    @Resource
    CommonParametersRepository commonParametersRepository;
    @Resource
    IConfigSmsFeign iSmsServiceFeign;


    /**
     * 查询短信公共参数列表
     * @param request
     * @author wrc
     * @date 2020/6/3
     */
    @Override
    public Wrapper<PageData<CommonParameters>> messageCommonList(PageModel request, Integer serviceType) {
        Pageable page = PageRequest.of(request.getCurrent(), request.getPageSize(), Sort.by("id").ascending());
        Page<CommonParameters> all = commonParametersRepository.findByServiceType(serviceType, page);
        return Wrapper.success(new PageData<>(all.getTotalElements(),all.getContent()));
    }

    /**
     * 添加公共参数
     * @param httpServletRequest
     * @param request
     * @author wrc
     * @date 2020/6/3
     */
    @Transactional
    @Override
    public Wrapper messageCommonAdd(HttpServletRequest httpServletRequest, CommonParametersAddRequest request) {
        List<CommonParameters> allByCodeEquals = commonParametersRepository.findAllByCodeEquals(request.getCode());
        List<CommonParameters> allByValueEquals = commonParametersRepository.findAllByValueEquals(request.getValue());
        if(null!=allByValueEquals&&allByValueEquals.size()>0){
            return Wrapper.fail("参数值已存在请不要重复添加");
        }
        if(null!=allByCodeEquals&&allByCodeEquals.size()>0){
            return Wrapper.fail("参数代码已存在请不要重复添加");
        }
        MiddlegroundUserLoginResponse loginUserDetails = userDetailsUtil.getLoginUserDetails(httpServletRequest);
        CommonParameters commonParameters=new CommonParameters();
        BeanUtils.copyProperties(request,commonParameters);
        commonParameters.setCreateTime(System.currentTimeMillis());
        commonParameters.setCreateRoleId(loginUserDetails.getId());
        commonParametersRepository.save(commonParameters);
        SmsConfigVO smsConfigVO=new SmsConfigVO();
        smsConfigVO.setCode(request.getCode());
        smsConfigVO.setRemark(request.getDescribe());
        smsConfigVO.setServiceType(request.getServiceType());
        smsConfigVO.setValue(request.getValue());
        iSmsServiceFeign.saveOrUpdateSmsConfig(smsConfigVO);
        return Wrapper.success();
    }

    /**
     * 修改参数
     *
     * @param request
     * @author wrc
     * @date 2020/6/3
     */
    @Override
    public Wrapper messageCommonUpdate(CommonParametersUpdateRequest request) {
        Optional<CommonParameters> byId = commonParametersRepository.findById(request.getId());
        if(!byId.isPresent()){
            return Wrapper.fail(ResponseCode.RECORDS_DON_T_EXIST);
        }
        CommonParameters commonParameters = byId.get();
        List<CommonParameters> allByCodeEquals = commonParametersRepository.findAllByCodeEquals(request.getCode());
        List<CommonParameters> allByValueEquals = commonParametersRepository.findAllByValueEquals(request.getValue());
        if(null!=allByValueEquals&&allByValueEquals.size()>0){
            Map<Long, CommonParameters> collect = allByCodeEquals.stream().collect(Collectors.toMap(CommonParameters::getId, v -> v));
            collect.remove(request.getId());
            if(null!=collect&&collect.size()>0){
                return Wrapper.fail("参数值已存在请不要重复添加");
            }
        }
        if(null!=allByCodeEquals&&allByCodeEquals.size()>0){
            Map<Long, CommonParameters> collect = allByCodeEquals.stream().collect(Collectors.toMap(CommonParameters::getId, v -> v));
            collect.remove(request.getId());
            if(null!=collect&&collect.size()>0){
                return Wrapper.fail("参数代码已存在请不要重复添加");
            }
        }
        BeanUtils.copyProperties(request,commonParameters);
        commonParameters.setUpdateTime(System.currentTimeMillis());
        commonParametersRepository.save(commonParameters);
        SmsConfigVO smsConfigVO=new SmsConfigVO();
        smsConfigVO.setCode(request.getCode());
        smsConfigVO.setRemark(request.getDescribe());
        smsConfigVO.setServiceType(request.getServiceType());
        smsConfigVO.setValue(request.getValue());
        iSmsServiceFeign.saveOrUpdateSmsConfig(smsConfigVO);
        return  Wrapper.success();
    }

    /**
     * 删除短信公共参数
     *
     * @param request
     * @author wrc
     * @date 2020/6/3
     */
    @Transactional
    @Override
    public Wrapper messageCommonDelete(CommonParametersDeteleRequest request) {
        CommonParameters commonParameters = commonParametersRepository.findById(request.getId()).orElse(null);
        if(commonParameters != null){
            commonParametersRepository.deleteById(request.getId());
            DeleteConfigRequest deleteConfigRequest = new DeleteConfigRequest();
            deleteConfigRequest.setCode(commonParameters.getCode());
            deleteConfigRequest.setServiceType(commonParameters.getServiceType());
            iSmsServiceFeign.deleteSmsConfig(deleteConfigRequest);
        }
        return Wrapper.success();
    }



}
